package com.lmy.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class RequestController {

    /**
     * 在request域对象中放入一个属性，并请求转发到/ok
     * @param request Servlet原生API设置request域对象属性
     * @return
     */
    @RequestMapping(value = "/request", method = RequestMethod.GET)
    public String requestAttribute(HttpServletRequest request) {
        //向request域中添加了数据
        request.setAttribute("user", "lmy");
        //请求转发到/ok
        return "forward:/ok";
    }

    /**
     * 使用@RequestAttribute获取request域对象的属性值
     * @param user 属性key
     * @param request Servlet原生API获取request域对象属性
     * @return
     */
    @RequestMapping(value = "/ok", method = RequestMethod.GET)
    @ResponseBody
    public String ok(@RequestAttribute(value = "user", required = false) String user,
                     HttpServletRequest request) {
        //获取request域中的数据
        System.out.println("user: " + user);
        System.out.println("user(通过HttpServletRequest获取): " + request.getAttribute("user"));
        return "success";
    }

    /**
     * 在request域对象中放入一个属性，并请求转发到/ok
     * @param request Servlet原生API设置session域对象属性
     * @return
     */
    @RequestMapping(value = "/session", method = RequestMethod.GET)
    public String sessionAttribute(HttpServletRequest request) {
        //向request域中添加了数据
        request.getSession().setAttribute("password", "123456");
        //请求转发到/ok2
//        return "forward:/ok2";
        //由于session域对象的值在一次会话中共享，所以重定向也可以获取其属性
        return "redirect:/ok2";
    }

    /**
     * 使用@SessionAttribute获取session域对象的属性值
     * @param password 属性key
     * @param request Servlet原生API获取session域对象属性
     * @return
     */
    @RequestMapping(value = "/ok2", method = RequestMethod.GET)
    @ResponseBody
    public String ok2(@SessionAttribute(value = "password", required = false) String password,
                     HttpServletRequest request) {
        //获取session域中的数据
        System.out.println("password: " + password);
        System.out.println("password(通过HttpServletRequest获取): " + request.getSession().getAttribute("password"));
        return "success";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register(Map<String, Object> map,
                           Model model,
                           HttpServletRequest request) {
        //如果有一个请求，会将请求数据封装到map中，或者model中
        //这里模拟请求参数
        map.put("user", "lmy");
        map.put("job", "java工程师");
        model.addAttribute("sal", 80000);
        //map和Model中的数据会被放入到request域对象中
        //转发到/registerOk
        return "forward:/registerOk";
    }

    @RequestMapping(value = "/registerOk", method = RequestMethod.GET)
    @ResponseBody
    public String registerOk(HttpServletRequest request) {
        Object user = request.getAttribute("user");
        Object job = request.getAttribute("job");
        Object sal = request.getAttribute("sal");
        System.out.println("user: " + user + ", job: " + job + ", sal: " + sal);
        return "success";
    }


}
